/*
 * Copyright (c) 2022 Hong Jiahua
 * https://gitee.com/arrco/jh_stack
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <stdio.h>

#include "jh_stack.h"
/************************************************************************/
/*                                                                      */
/************************************************************************/
//数据类型为int，从栈底按从小到大的方式排序：
int compare(const void* a, const void* b) {
     return ( *(int*)a - *(int*)b );
}

int main(int argc, char* argv[])
{
    //定义数据结构和用于存放数据的数组
    jh_stack_t stack;
    
    #ifdef JH_STACK_DYNAMIC_SPACE
    //动态内存空间
    //栈空间申请
    jh_stack_space_malloc(&stack, 10, sizeof(int));
    #else
    //静态内存空间
    int buf[10];
    
    //栈初始化
    jh_stack_init(&stack, buf, 10, sizeof(int));
    #endif
    
    //定义要用于传入的数据
    int items[] = {81, 13, 16, 38, 49, 67};
    int i, val;
    
    //数据入栈
    printf("push :\n");
    for(i = 0; i < sizeof(items)/sizeof(int); i++) {
        if(!jh_stack_push(&stack, &items[i]))
            printf("%d ", items[i]);
    }
    printf("\n");
    
    //查看当前的状态
    printf("\nsize : %zu empty : %d full : %d\n", jh_stack_count(&stack), jh_stack_is_empty(&stack), jh_stack_is_full(&stack));
    
    //查看栈顶的数据
    jh_stack_peek(&stack, &val);
    printf("\npeek %d\n", val);
    
    //查看栈底的数据
    jh_stack_peek_bottom(&stack, &val);
    printf("\npeek bottom %d\n", val);
    
    //数据出栈
    printf("\npop :\n");
    for(i = 0; i < sizeof(items)/sizeof(int); i++) {
        if(!jh_stack_pop(&stack, &val))
            printf("%d ", val);
    }
    printf("\n");

    //查看当前的状态
    printf("\nsize : %zu empty : %d full : %d\n", jh_stack_count(&stack), jh_stack_is_empty(&stack), jh_stack_is_full(&stack));
    
    
    
    //数据入栈
    printf("\npush :\n");
    for(i = 0; i < sizeof(items)/sizeof(int); i++) {
        if(!jh_stack_push(&stack, &items[i]))
            printf("%d ", items[i]);
    }
    printf("\n");
    
    //可以查看当前所有数据，从栈顶到栈底的顺序遍历
    printf("\niter:\n");
    JH_STACK_ITER(&stack, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //可以反向查看当前所有数据，从栈底到栈顶的顺序遍历
    printf("\niter reverse:\n");
    JH_STACK_ITER_REVERSE(&stack, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //排序
    jh_stack_sort(&stack, compare);
    
    //可以查看当前所有数据，从栈顶到栈底的顺序遍历
    printf("\niter:\n");
    JH_STACK_ITER(&stack, &val) {
        printf("%d ", val);
    }
    printf("\n");
    
    //清空栈
    jh_stack_clear(&stack);
    
    //查看当前的状态
    printf("\nsize : %zu empty : %d full : %d\n", jh_stack_count(&stack), jh_stack_is_empty(&stack), jh_stack_is_full(&stack));
    
    #ifdef JH_STACK_DYNAMIC_SPACE
    //栈空间释放
    jh_stack_space_free(&stack);
    #endif
    
    return 0;
}